#include <QCoreApplication>
#include <iostream>
#include <vector>
#include <ctime>
#include <cstdlib>
#include <string>

using namespace std;

void pr_3_31()
{
    int ia[10];
    for (size_t i = 0; i < 10; i++)
    {
        ia[i] = i;
    }
    for (auto iv : ia)
    {
        cout << iv << " ";
    }
    cout << endl;
}

void pr_3_32()
{
    int ia[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    int ib[10];
    for (size_t i = 0; i < 10; i++)
    {
        ib[i] = ia[i];
    }
    for (auto iv : ib)
    {
        cout << iv << " ";
    }
    cout << endl;

    cout << "use vector" << endl;
    vector<int> iva;
    vector<int> ivb;
    for (auto i = 0; i < 10; i++)
    {
        iva.push_back(i);
    }
    ivb = iva;
    for (auto iv : ivb)
    {
        cout << iv << " ";
    }
    cout << endl;
}

void pr_3_35()
{
    int ia[] = {0,1,2,3,4,5,6,7,8,9};
    auto *pbeg = begin(ia), *pend = end(ia);
    cout << "before" << endl;
    for (auto iv : ia) {
        cout << iv << " ";
    }
    cout << endl;
    while (pbeg != pend) {
        *pbeg = 0;
        ++pbeg;
    }
    cout << "after" << endl;
    for (auto iv : ia) {
        cout << iv << " ";
    }
    cout << endl;
}

void pr_3_36_1()
{
    // 比较两个数组是否相等
    int arr1[] = {0,1,2,3,4,5,6,7,8,9};
    int arr2[] = {0,1,2,3,4,5,6,7,8,10};

    if (sizeof(arr1) != sizeof(arr2)) {
        cout << "arr1 != arr2" << endl;
        return;
    }
    for (size_t i = 0; i < sizeof(arr1) / sizeof(int); i++)
    {
        if (arr1[i] != arr2[i])
        {
            cout << "arr1 != arr2" << endl;
            return;
        }
    }
    cout << "arr1 == arr2" << endl;
}

void pr_3_36_2()
{
    // 比较两个 vector是否相等
    vector<int> vec1;
    vector<int> vec2;
}

void pr_3_36_3()
{
    const int sz = 5; // 常量 sz作为数组的维度
    int a[sz], b[sz], i;
    srand((unsigned)time(NULL)); // 生成随机数种子
    // 通过 for循环为数组元素赋值
    for (i = 0; i < sz; i++) {
        a[i] = rand() % 10; // 每次循环生成一个 10以内的随机数并添加到 a中
    }
    cout << "系统数据已经生成，请输入您猜测的 5个数字（0~9),可以重复：" << endl;
    int uVal;
    // 通过 for循环为数组元素赋值
    for (i = 0; i < sz; i++) {
        if (cin >> uVal) {
            b[i] = uVal;
        }
    }
    cout << "系统生成的数据是：" << endl;
    for (auto val : a) {
        cout << val << " ";
    }
    cout << endl;
    cout << "您猜测的数据是：" << endl;
    for (auto val : b) {
        cout << val << " ";
    }
    cout << endl;
    int *p = begin(a), *q = begin(b); // 令p和q分别指向数组a和b的首元素
    while (p != end(a) && q != end(b))
    {
        if (*p != *q)
        {
            cout << "您的猜测错误：两个数组不相等" << endl;
            return;
        }
        p++; // p向后移动一位
        q++; // q向后移动一位
    }
    cout << "恭喜你，全部猜对了!!!" << endl;
    return;
}

void pr_3_36_4()
{
    const int sz = 5; // 常量sz作为vector的容量
    vector<int> a, b;
    srand((unsigned)time(NULL)); // 生成随机数种子
    // 为vector元素赋值
    for (auto i = 0; i < sz; ++i) {
        a.push_back(rand() % 10); // 每次循环生成一个 10以内的随机数并添加到 a中
    }
    cout << "系统数据已经生成,请输入你猜测的5个数字(0~9),可以重复: " << endl;
    int uVal;
    // 为 vecotr元素赋值
    for (auto i = 0; i < sz; ++i) {
        if (cin >> uVal) {
            b.push_back(uVal);
        }
    }
    cout << "系统生成的数据是：" << endl;
    for (auto val : a) {
        cout << val << " ";
    }
    cout << endl;
    cout << "您猜测的数据是：" << endl;
    for (auto val : b) {
        cout << val << " ";
    }
    cout << endl;
    // 令 it1,it2分别指向 vector对象 a和b的首元素
    auto it1 = a.cbegin(), it2 = b.cbegin();
    while (it1 != a.cend() && it2 != b.cend()) {
        if (*it1 != *it2) {
            cout << "您的猜测错误，两个vector不相等" << endl;
            return;
        }
        it1++; // it1向后移动一位
        it2++; // it2向后移动一位
    }
    cout << "恭喜您全部猜对了." << endl;
    return;
}

void pr_3_37()
{
    const char ca[] = {'h', 'e', 'l', 'l', 'o'};
    const char *cp = ca;
    while (*cp) {
        cout << *cp << endl;
        ++cp;
    }
}

/**
 * @brief 比较两个string字符串
 */
void pr_3_39_1()
{
    string s1, s2;
    cout << "请输入两个字符串:" << endl;
    cin >> s1 >> s2;
    if (s1 == s2) {
        cout << "s1 = s2" << endl;
    } else if (s1 < s2) {
        cout << "s1 < s2 " << endl;
    } else {
        cout << "s1 > s2" << endl;
    }
}

/**
 * @brief 比较两个C风格字符串
 */
void pr_3_39_2()
{
    char a[80], b[80];
    cout << "请输入两个字符串 " << endl;
    cin >> a >> b;
    auto cmp_result = strcmp(a, b);
    if (cmp_result == 0) {
        cout << "a = b" << endl;
    } else if (cmp_result < 0) {
        cout << "a < b" << endl;
    } else {
        cout << "a > b" << endl;
    }
}

/**
 * @brief 拼接C风格字符串
 */
void pr_3_40()
{
    const char str1[] = "hello";
    const char str2[] = " world";
    // strlen计算得到的长度比实际多 1
    char str3[strlen(str1) + strlen(str2) - 1];
    strcpy(str3, str1);

    strcat(str3, str2);
    cout << str3 << endl;
}

/**
 * @brief 用整型数组初始化一个vector对象
 */
void pr_3_41()
{
    const int sz = 10;
    int arry[sz];
    for (size_t i = 0; i < sz; i++) {
        arry[i] = i;
    }
    vector<int> vec(begin(arry), end(arry));
    for (auto i : vec) {
        cout << i << " ";
    }
    cout << endl;
}

/**
 * @brief 将vector对象的元素拷贝给数组
 */
void pr_3_42()
{
    const int sz = 10;
    vector<int> iVec;
    srand((unsigned)time(NULL));
    // 填充vector
    for (auto i = 0; i < sz; i++) {
        iVec.push_back(rand() % 10);
    }
    int iArr[sz];
    for (auto i = 0; i < sz; i++) {
        iArr[i] = iVec[i];
    }
    // 遍历数组
    for (auto i : iArr) {
        cout << i << " ";
    }
    cout << endl;
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    pr_3_42();

    return a.exec();
}
